package dobest.ray.com.jiongjiong.views.seekbar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.dobest.ray.corelibs.utils.Tools;

import java.math.BigDecimal;

import dobest.ray.com.jiongjiong.R;


public class SeekBarLayout extends LinearLayout {
	private Context context;
	private MySeekbarCanMove bar_main01_heredity;
	// private TextMoveLayout textLayout_main01_heredity;
	private ScaleCallBack callback;
	private RelativeLayout rl_content;

	private TextView tv_text1, tv_text2, tv_text3, tv_show_text;
	private int offest = 0;

	public SeekBarLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		init();
	}

	public SeekBarLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	public SeekBarLayout(Context context) {
		super(context);
		this.context = context;
		init();
	}

	private void init() {
		View view = LayoutInflater.from(context).inflate(
				R.layout.seekbar_layout, this);
		bar_main01_heredity = (MySeekbarCanMove) view
				.findViewById(R.id.bar_main01_heredity);
		// textLayout_main01_heredity = (TextMoveLayout) view
		// .findViewById(R.id.textLayout_main01_heredity);
		rl_content = (RelativeLayout) findViewById(R.id.rl_content);
		tv_show_text = (TextView) findViewById(R.id.tv_show_text);
		tv_text1 = (TextView) findViewById(R.id.tv_text1);
		tv_text2 = (TextView) findViewById(R.id.tv_text2);
		tv_text3 = (TextView) findViewById(R.id.tv_text3);

		bar_main01_heredity
				.setOnSeekBarChangeListener(new OnSeekBarChangeListenerImp());
		tv_text1.post(new Runnable() {

			@Override
			public void run() {
				offest = tv_text1.getWidth();
				RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tv_show_text
						.getLayoutParams();
				offest -= 5;
				params.setMargins(offest, 0, 0, 0);
				tv_show_text.setLayoutParams(params);
			}
		});
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	private class OnSeekBarChangeListenerImp implements
			SeekBar.OnSeekBarChangeListener {
		private int progress;

		// 触发操作，拖动
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			this.progress = progress;
			move(progress);
		}

		// 表示进度条刚开始拖动，开始拖动时候触发的操作
		public void onStartTrackingTouch(SeekBar seekBar) {

		}

		// 停止拖动时候
		public void onStopTrackingTouch(SeekBar seekBar) {
			if (callback != null) {
				callback.onScaleChanged(progress);
			}
			// if (progress > 0 && progress < 13) {
			// seekBar.setProgress(0);
			// }
			// if (progress > 12 && progress < 38) {
			// seekBar.setProgress(25);
			// }
			// if (progress > 37 && progress < 63) {
			// seekBar.setProgress(50);
			// }
			// if (progress > 62 && progress < 88) {
			// seekBar.setProgress(75);
			// }
			// if (progress > 87) {
			// seekBar.setProgress(100);
			// }
		}
	}

	public void setData(int scale, String text1, String text2, String text3) {
		tv_text1.setText(text1);
		tv_text2.setText(text2);
		tv_text3.setText(text3);
		bar_main01_heredity.setProgress(scale);
	}

	private boolean isSpecial = false;
	private float min = 0;
	private float max = 0;

	public void setSpecialData(boolean isSpecial, float min, float max) {
		this.isSpecial = isSpecial;
		this.max = max;
		this.min = min;
		tv_show_text.setText(min == 0 ? "0" : min + "");
	}

	public void setScaleCallback(ScaleCallBack callback) {
		this.callback = callback;
	}

	public interface ScaleCallBack {
		void onScaleChanged(int scale);
	}

	private void move(int progress2) {
		int width = bar_main01_heredity.getWidth() - Tools.dip2px(context, 10);
		float moveStep = (float) (((float) width / (float) 100) * 0.88);

		RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tv_show_text
				.getLayoutParams();

		params.setMargins((int) (progress2 * moveStep) + offest, 0, 0, 0);
		tv_show_text.setLayoutParams(params);
		if (isSpecial) {
			BigDecimal b = new BigDecimal((progress2 / 100.0f) * (max - min));
			tv_show_text.setText((b.setScale(1, BigDecimal.ROUND_HALF_UP)
					.floatValue() + min) + "");
		} else {
			tv_show_text.setText("" + progress2);
		}
	}
}
